<template>
  <el-dialog :title="!dataForm.id ? '新建' :  isDetail ? '详情':'编辑'" :close-on-click-modal="false" :visible.sync="visible"
    lock-scroll width="800px">
    <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right"
      :disabled="!!isDetail" :rules="rules">
      <el-col :span="12">
        <el-form-item label="咨询时间" prop="time">
          <el-date-picker v-model="dataForm.time" placeholder="请选择" clearable required :style='{"width":"100%"}'
            type='datetime' format="yyyy-MM-dd HH:mm:ss" value-format="timestamp">
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="咨询渠道" prop="channel">
          <el-select v-model="dataForm.channel" placeholder="请选择" clearable required :style="{ width: '100%' }"
            filterable>
            <el-option-group v-for="group in channelOptions" :key="group.id" :label="group.fullName">
              <el-option v-for="item in group.children" :key="item.id" :label="item.fullName"
                :value="item.enCode"></el-option>
            </el-option-group>
          </el-select>
          <!-- <el-select v-model="dataForm.consultWay" placeholder="请选择" clearable required :style='{"width":"100%"}'
              filterable>
              <el-option v-for="(item, index) in consultWayOptions" :key="index" :label="item.fullName"
                :value="item.id"></el-option>
            </el-select> -->
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="咨询内容" prop="content">
          <el-input v-model="dataForm.content" placeholder="请输入" required show-word-limit :style='{"width":"100%"}'
            type='textarea' :autosize='{"minRows":4,"maxRows":4}'>
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="聊天截图" prop="chatScreen">
          <custom-upload-img v-model="dataForm.chatScreen" :fileSize="5" sizeUnit="MB" :limit="9"
            type="consult"></custom-upload-img>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('common.cancelButton')}}</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail" :loading="loading">
        {{$t('common.confirmButton')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { createConsult, updateConsult, getConsultInfo } from '@/api/crm/consult'
export default {
  components: {},
  props: [],
  data () {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: undefined,
        customerId: undefined,
        time: undefined,
        channel: undefined,
        content: undefined,
        chatScreen: [],
      },
      rules: {
        time: [
          {
            required: true,
            message: '请输入咨询时间',
            trigger: 'change'
          },
        ],
        channel: [
          {
            required: true,
            message: '请输入咨询渠道',
            trigger: 'change'
          },
        ],
        content: [
          {
            required: true,
            message: '请输入咨询内容',
            trigger: 'blur'
          },
        ],
      },
      channelOptions: [],
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getChannelOptions();
  },
  mounted () {
  },
  methods: {
    getChannelOptions () {
      this.$store.dispatch('base/getDictionaryData', { sort: 'ConsultChannels' }).then(res => {
        this.channelOptions = this.isDetail ? res : res.filter(o => o.enabledMark == 1)
      })
    },
    goBack () {
      this.$emit('refresh')
    },
    init (id, isDetail) {
      this.dataForm.id = id || ''
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields();
        if (this.dataForm.id) {
          getConsultInfo(this.dataForm.id).then(res => {
            this.dataForm = res.data;
          })
        }
      })
    },
    dataFormSubmit () {
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          const formMethod = this.dataForm.id ? updateConsult : createConsult
          formMethod(this.dataForm).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.loading = false
                this.$emit('refresh', true)
              }
            })
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
  }
}
</script>
